<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="条码规则编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                readonly
                v-decorator="[ 'ruleNo', validatorRules.ruleNo]"
                placeholder="请输入条码规则编号"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="条码规则名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'ruleName', validatorRules.ruleName]" placeholder="请输入条码规则名称"></p-input>
            </p-form-item>
            <p-form-item label="条码生成类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <p-input  v-decorator="[ 'codeType', validatorRules.codeType]" placeholder="请输入条码生成类型"></p-input> -->
              <j-dict-select-tag
                type="list"
                v-decorator="['codeType', validatorRules.codeType]"
                :trigger-change="true"
                dictCode="	mes_barcode_pruduce_code_type"
                placeholder="请输入条码生成类型"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="条码长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number
                v-decorator="[ 'length', validatorRules.length]"
                placeholder="请输入条码长度"
                style="width: 100%"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="条码规则描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'ruleSprc', validatorRules.ruleSprc]" placeholder="请输入条码规则描述"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="条码规则类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['ruleType', validatorRules.ruleType]"
                :trigger-change="true"
                dictCode="mes_barcode_rule_type"
                placeholder="请选择条码规则类型"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="网址数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number
                v-decorator="[ 'macQty', validatorRules.macQty]"
                placeholder="请输入网址数"
                style="width: 100%"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-switch
                placeholder="String"
                v-decorator="['status']"
                field="status"
                dictCode="mes_open_close"
                @callback="popupCallback"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import { httpAction, putAction, api } from "@/api/manage";
import pick from "lodash.pick";
import fSwitch from "@/components/switch";
import JDictSelectTag from "@/components/dict/JDictSelectTag";

export default {
  name: "MesBcRuleModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JDictSelectTag,
    fSwitch,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        ruleNo: { rules: [{ required: true, message: "请输入条码规则编号!" }] },
        codeType: { rules: [{ required: true, message: "请输入条码规则编号!" }] },
        ruleName: {
          rules: [{ required: true, message: "请输入条码规则名称!" }],
        },
        length: { rules: [{ required: true, message: "请输入条码长度!" }] },
        ruleSprc: {
          rules: [{ required: true, message: "请输入条码规则描述!" }],
        },
        ruleType: {
          rules: [{ required: true, message: "请输入条码规则类型!" }],
        },
        macQty: {},
        status: {},
        remark: {},
      },
      url: {
        add: "/mes-code/mesBcRule",
        edit: "/mes-code/mesBcRule",
      },
    };
  },
  created() {},
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [{ ruleCode: "mes_barcode_rule_no", columnName: "ruleNo" }],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
          const item = res.data;
          this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "ruleNo",
            "ruleName",
            "length",
            "ruleSprc",
            "ruleType",
            "ruleFiled",
            "ruleSample",
            "macQty",
            "status",
            "remark",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            'codeType'
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "ruleNo",
          "ruleName",
          "length",
          "ruleSprc",
          "ruleType",
          "ruleFiled",
          "ruleSample",
          "macQty",
          "status",
          "remark",
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          'codeType'
        )
      );
    },
  },
};
</script>